﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
//using NUnit.Framework;

namespace examprojekt09
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        
        // Denne vil gerne være statik, ellers går den AMOK! :D
        static DataSet ds = DataMapper.Instance.getAllSqlData();
        List<Project> projects = DataMapper.Instance.getProjects(ds);
        List<Employee> empList = DataMapper.Instance.getEmployees(ds);
        public Window1()
        {
            InitializeComponent();
            this.start();
            this.loadCP();
          
            
        }
     
        private void start()
        {
            
            this.pm_projectList.Items.Clear();
            //List<Project> projects = Crudbackend.Instance.getProjects();
            foreach (Project pro in projects)
            {
                this.pm_projectList.Items.Add(pro);
            }

            foreach (Employee employee in empList)
            {
                if (employee.getPid() == -1)
                {
                    this.pm_avaEmp.Items.Add(employee);
                }
            }

        }

        private void pm_projectList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.pm_projectDesc.Clear();
                this.pm_occuEmp.Items.Clear();
                Project project = (Project)pm_projectList.SelectedItem;
                this.pm_projectDesc.AppendText(project.getProjectDesc());
                int projectId = project.getId();

                foreach (Employee employee in empList)
                {
                    if (projectId == employee.getPid())
                    {
                        this.pm_occuEmp.Items.Add(employee);
                    }
                }

            }
            catch (NullReferenceException)
            {
                //this.label2.Content = "hvaaaaaaaaaad";
            }
        }

        private void pm_avaEmp_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Project project = (Project)pm_projectList.SelectedItem;
                Employee employee = (Employee)pm_avaEmp.SelectedItem;
                int projectID = project.getId();
                this.pm_avaEmp.Items.Clear();
                this.pm_occuEmp.Items.Clear();
                int empId = employee.getId();
                DataMapper.Instance.updateEmployeePid(empId, projectID, ds);

                foreach (Employee emp in empList)
                {
                    if (emp.getId() == employee.getId())
                    {
                        emp.setPid(projectID);
                    }
                    if (projectID == emp.getPid())
                    {
                        this.pm_occuEmp.Items.Add(emp);
                    }
                    if (emp.getPid() == -1)
                    {
                        this.pm_avaEmp.Items.Add(emp);
                    }
                }

            }
            catch (NullReferenceException)
            {
                //this.label2.Content = "hvaaaaaaaaaad";
            }
        }
        private void pm_occuEmp_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Project project = (Project)pm_projectList.SelectedItem;
                Employee employee = (Employee)pm_occuEmp.SelectedItem;
                int projectID = project.getId();
                int availableEmployee = -1;
                int empId = employee.getId();
                this.pm_avaEmp.Items.Clear();
                this.pm_occuEmp.Items.Clear();
                DataMapper.Instance.updateEmployeePid(empId, availableEmployee, ds);

                foreach (Employee emp in empList)
                {
                    if (emp.getId() == employee.getId())
                    {
                        emp.setPid(availableEmployee);
                    }
                    if (projectID == emp.getPid())
                    {
                        this.pm_occuEmp.Items.Add(emp);
                    }
                    if (emp.getPid() == -1)
                    {
                        this.pm_avaEmp.Items.Add(emp);
                    }
                }

            }
            catch (NullReferenceException)
            {
                //this.label2.Content = "hvaaaaaaaaaad";
            }
        }
        private void loadCP()
        {
            // Adding days, monts and years to the dropdown lists in Create Project.
            Array reqemp = new int[] { 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
            Array days = new int[] {01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
            Array month = new int[] {01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12};
            Array year = new int[] {2009, 2010, 2011, 2012, 2013, 2014, 2015};
            foreach (int i in reqemp)
            {
                this.pc_reqemp.Items.Add(i);
            }
            foreach (int i in days)
            {
                this.pc_dayStart.Items.Add(i);
                this.pc_dayEnd.Items.Add(i);
            }
            foreach (int i in month)
            {
                this.pc_monthStart.Items.Add(i);
                this.pc_monthEnd.Items.Add(i);
            }
            foreach (int i in year)
            {
                this.pc_yearStart.Items.Add(i);
                this.pc_yearEnd.Items.Add(i);
            }
            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string pName = this.pc_name.Text;
            string pDetail = this.pc_detail.Text;
            int reqEmployee = (int)this.pc_reqemp.SelectedItem;
            int dayStart = (int)pc_dayStart.SelectedItem;
            int monthStart = (int)pc_monthStart.SelectedItem;
            int yearStart = (int)pc_yearStart.SelectedItem;
            int dayEnd = (int)pc_dayEnd.SelectedItem;
            int monthEnd = (int)pc_monthEnd.SelectedItem;
            int yearEnd = (int)pc_yearEnd.SelectedItem;

            string startDate = "" + dayStart + monthStart + yearStart;
            string endDate = "" + dayEnd + monthEnd + yearEnd;
            int cp = DataMapper.Instance.createProject(ds, pName, pDetail, startDate, endDate, reqEmployee);
            MessageBox.Show("Project " + pName + " was added succesfully.");
            this.projects.Add(new Project(cp, pName, pDetail, startDate, endDate, reqEmployee));
            this.start();
        }

        void Window1_Closing(object sender, CancelEventArgs e)
        {
            DataMapper.Instance.commitDataSet(ds);
        }

        private void ec_save_Click(object sender, RoutedEventArgs e)
        {
            string name = this.ec_name.Text;
            string email = this.ec_email.Text;
            int pid = -1;
            int ce = DataMapper.Instance.createEmployee(ds, name, email, pid);
            MessageBox.Show("Emplyee " + name + " was added succesfully.");
            this.empList.Add(new Employee(ce, name, email, -1));
            this.start();
        }

        
    }
}
